import echoMenu from './echo-menu'
import echoTool from './echo-tool'
import {echo} from './echo'
class ControlBox {

  /*
   ECHO:
   depth: 水深值,单位m
   depthAlarm: 报警水深值,单位m
   power: 开关机状态, [on, off]
   */

  /*
   window.$controlbox.setValue({
   lat: 1234.32,
   lon: 1234.3
   })
   */

  // window.$controlbox.getValue(['lat', 'lon'])

  // keys: Array
  getValue (keys) {
    let arr = []

    for (let i = 0; i < keys.length; i++) {
      switch (keys[i]) {
        case 'depth':
          let len = echoTool.EchoData.length
          if (len === 0) {
            arr.push(0)
          } else {
            arr.push(echoTool.EchoData[len - 1].depth)
          }

          break
        case 'depthAlarm':
          arr.push(echoMenu.menu[1].value)
          break
        case 'power':
          if (echo.poweron) {
            arr.push('on')
          } else {
            arr.push('off')
          }
          break
      }
    }
    return arr
  }

  getAllValues () {
    let len = echoTool.EchoData.length
    let depth = 0
    if (len !== 0) {
      depth = echoTool.EchoData[len - 1].depth
    }

    let _pwd
    if (echo.poweron) {
      _pwd = 'on'
    } else {
      _pwd = 'off'
    }
    return {
      depth: depth,
      depthAlarm: echoMenu.menu[1].value,
      power: _pwd
    }
  }

  // values: Object
  setValue (values) {
    console.log('echo set Value = ' + values)

    if (values.lat !== undefined) {
      echo.oship.lat = values.lat
    }

    if (values.lon !== undefined) {
      echo.oship.lon = values.lon
    }

    if (values.cog !== undefined) {
      echo.oship.cog = values.cog
    }

    if (values.sog !== undefined) {
      echo.oship.sog = values.sog
    }

    if (values.depth !== undefined) {
      let val = values.depth.toFixed(1)
      echoTool.depth = parseFloat(val)
    }

    if (values.depthAlarm !== undefined) {
      echoMenu.menu[1].value = values.depthAlarm
    }

    if (values.power !== undefined) {
      if (values.power === 'on') {
        echo.$echo.poweron = true
      } else {
        echo.$echo.poweron = false
      }
    }
  }

  callFunc (func, args = undefined) {
  }

}

window.$controlbox = new ControlBox()

